use std::sync::{Arc, Mutex};
use crate::AppState;
use crate::config::Config;
use async_static::async_static;
use crate::daoimpl::Database;

pub mod login_service;
pub mod goods_service;
pub mod cust_order_service;
pub mod pay_type_service;
pub mod status_service;
pub mod order_item_service;
pub mod goods_type_service;
pub mod recommend_service;
pub mod role_service;
pub mod permission_service;
pub mod user_service;

async_static! {
		static ref APP_STATE:AppState<'static> = init_db_inner().await;
}

pub async fn init_db() -> &'static AppState<'static> {
	let app_state = APP_STATE.await;
	return app_state;
}

pub async fn init_db_inner() -> AppState<'static>{
	let config_file: &'static str = "config.json";
	let config = Config::from_file(config_file);
	let db_context = Database::new(&config.get_database_url()).await;

	let app_state = AppState {
		connections: Mutex::new(0),
		context: Arc::new(db_context),
	};
	app_state
}
